﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using bianzhuo.SupportPlatform.BLL;
using bianzhuo.SupportPlatform.COL;
using bianzhuo.SupportPlatform.SFL;

namespace bianzhuo.SupportPlatform.WebUI.BzBusiness
{
    public partial class Business_AdminCreate1 : System.Web.UI.Page
    {
        private BusinessAdmin businessAdmin;

        /// <summary>
        /// “商家管理员模块”添加信息页面载入时的事件处理方法。
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            businessAdmin = new BusinessAdmin();
            BzBusiness.BusinessBase.LoginStatusChechk();
            HttpCookie bzBusiness = Request.Cookies["BzSupport_Business"];
            string BusAdminID = bzBusiness.Values["BusinessAdminID"].ToString();
            string BusinessID = bzBusiness.Values["BusinessID"].ToString();

            COL.BusinessAdmin admin = BLL.BusinessAdminBLL.GetDataByBusAdminID(BusAdminID);
            if (admin.IsBoss == false)
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "alert", "<script>alert('无权添加管理员');history.go(-1);</script>");
                return;
            }
            if (!Page.IsPostBack)
            {
                BindObjectToForm();  // 将关联对象默认值，绑定至界面控件进行显示。
            }
        }

        /// <summary>
        /// 将界面控件中的值，绑定给关联对象。
        /// </summary>
        private void BindFormlToObject()
        {
            HttpCookie bzBusiness = Request.Cookies["BzSupport_Business"];
            string BusAdminID = bzBusiness.Values["BusinessAdminID"].ToString();
            string BusinessID = bzBusiness.Values["BusinessID"].ToString();


            businessAdmin.LoginName = DataValid.GetNullOrString(TxtLoginName.Value);  // 商家管理员登录名
            if (TxtLoginPassword.Value != null && TxtLoginPassword.Value.Length > 0)
            {
                businessAdmin.LoginPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(TxtLoginPassword.Value, "MD5");
            }
            else
            {
                businessAdmin.LoginPassword = DataValid.GetNullOrString(TxtLoginPassword.Value);  // 商家管理员登录密码
            }
            //businessAdmin.BusinessPhone = DataValid.GetNullOrString(TxtBusinessPhone.Value);  // 商家管理员电话
            businessAdmin.Pic = pic.Value;
            businessAdmin.NickName = nickname.Value;
            businessAdmin.State = "1";  // 状态
            businessAdmin.AddTime = DateTime.Now;  // 添加时间
            businessAdmin.BelongsBusiness = MerchantInfoBLL.GetDataByMerchantId(int.Parse(BusinessID));  // 所属商家
        }

        /// <summary>
        /// 将关联对象中的值，绑定至界面控件进行显示。
        /// </summary>
        private void BindObjectToForm()
        {

            TxtLoginName.Value = businessAdmin.LoginName;  // 商家管理员登录名
            TxtLoginPassword.Value = businessAdmin.LoginPassword;  // 商家管理员登录密码
            //TxtBusinessPhone.Value = businessAdmin.BusinessPhone;  // 商家管理员电话
            //TxtState.Value = businessAdmin.State;  // 状态

            nickname.Value = businessAdmin.NickName;
            pic.Value = businessAdmin.Pic;

        }

        /// <summary>
        /// 用户单击“保存”按钮时的事件处理方法。
        /// </summary>
        protected void submitClick(object sender, EventArgs e)
        {
            string sql = "select count(*) from BusinessAdmin where LoginName='" + TxtLoginName.Value + "'";
            if (Convert.ToInt32(SFL.SqlHelper.GetSingle(sql)) > 0)
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "save", "<script>alert('登录名已经被使用，请换个。');</script>");
                return;
            }
            BindFormlToObject();
            businessAdmin.State = Request.Params["sltState"];
            int id = BusinessAdminBLL.Insert(businessAdmin);
            string editqx = Request.Params["chkbianji"] == null ? "0" : "1";
            string delqx = Request.Params["chkshanchu"] == null ? "0" : "1";
            string editqxtwo = Request.Params["chkbianjijlx"] == null ? "0" : "1";
            string sedtime = SEditTime.Value;
            string sqlx = string.Format("update BusinessAdmin set AEditQx='{0}',ADelQx='{1}',AEditQxTwo='{2}',AEditTime='{3}' where BusAdminID={4}", editqx, delqx, editqxtwo, sedtime, businessAdmin.BusAdminID);
            SqlHelper.GetSingle(sqlx);

            Page.ClientScript.RegisterStartupScript(GetType(), "save", "<script>alert('添加成功');location='Business_AdminDefault.aspx?BusAdminID=" + businessAdmin.BusAdminID + "'</script>");

        }

    }
}